package model
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	import spark.components.TextInput;
	
	public class SearchBar extends MovieClip 
	{

		public var keywordInputBg:TextInputBg;
		public var locationInputBg:TextInputBg;
		public var locationInputBg_long:LocationInputBg_long;
		public var searchBarMC:SearchBarMC;
		public var keywordTextInput:TextField;
		public var locationTextInput:TextField;
		
		public function SearchBar()
		{			
			searchBarMC = new SearchBarMC();
			keywordInputBg = new TextInputBg();
			locationInputBg = new TextInputBg();
			locationInputBg_long = new LocationInputBg_long();
			keywordTextInput = new TextField();
			keywordTextInput.type = TextFieldType.INPUT;
			
			keywordTextInput.text = "Keyword";
			keywordTextInput.setTextFormat(GlobalTextFormat.searchBarStyle);
			
			locationTextInput = new TextField();
			locationTextInput.type = TextFieldType.INPUT;
			
			locationTextInput.text = "Location (Optional)";
			locationTextInput.setTextFormat(GlobalTextFormat.searchBarStyle);
			
			addChild(searchBarMC);
			addChild(keywordInputBg);
			addChild(locationInputBg);
			addChild(locationInputBg_long);
			addChild(keywordTextInput);
			addChild(locationTextInput);		
			
			keywordTextInput.addEventListener(MouseEvent.CLICK, keywordTextInputHandler);
			//keywordTextInput.addEventListener(KeyboardEvent.KEY_DOWN, keywordOnKeyDown);
			locationTextInput.addEventListener(MouseEvent.CLICK, locationTextInputHandler);
		}
		
		public function showLocationInputOnly():void
		{
			keywordInputOption(148, 8, 8, false);
			locationInputOption(148, 163, 8, false);
			
			locationInputBg_long.x = 8;
			locationInputBg_long.y = 8;
			locationInputBg_long.width = 302;
			locationInputBg_long.visible = true;
			locationTextInput.x = locationInputBg_long.x + 5;
			locationTextInput.y = locationInputBg_long.y + 5;
			locationTextInput.width = locationInputBg_long.width - 10;
			locationTextInput.height = 20;
			//locationTextInput.border = true;
			locationTextInput.visible = true;
		}
		
		public function showKeywordLocationInput():void
		{
			locationInputBg_long.visible = false;
			keywordInputOption(148, 8, 8, true);
			locationInputOption(148, 163, 8, true);
		}
		
		public function keywordInputOption(w:Number, x:Number, y:Number, visible:Boolean):void
		{
			keywordInputBg.width = w;
			keywordInputBg.x = x;
			keywordInputBg.y = y;
			keywordInputBg.visible = visible;
			keywordTextInput.x = x + 5;
			keywordTextInput.y = y + 5;
			keywordTextInput.width = w - 10;
			keywordTextInput.height = 20;
			//keywordTextInput.border = true;
			keywordTextInput.visible = visible;
		}
		
		public function locationInputOption(w:Number, x:Number, y:Number, visible:Boolean):void
		{
			locationInputBg.width = w;
			locationInputBg.x = x;
			locationInputBg.y = y;
			locationInputBg.visible = visible;
			locationTextInput.x = x + 5;
			locationTextInput.y = y + 5;
			locationTextInput.width = w - 10;
			locationTextInput.height = 20;
			//locationTextInput.border = true;
			locationTextInput.visible = visible;
		}
		
		private function keywordTextInputHandler(event:MouseEvent):void
		{
			if(keywordTextInput.text == "Keyword"){
				keywordTextInput.text = "";
				keywordTextInput.textColor = 0x000000;
			}
			
			keywordTextInput.dispatchEvent(new Event("KEYWORD_SELECTED"));
		}
		private function keywordOnKeyDown(event:KeyboardEvent):void
		{
			if(event.keyCode == 13)
			{
				//trace("enter key = ");
				
			}//var char:String = String.fromCharCode(event.charCode).toUpperCase();
			keywordTextInput.dispatchEvent(new KeyboardEvent("KEYWORD_DOWN"));
			
		}
		private function locationTextInputHandler(event:MouseEvent):void
		{
			if(locationTextInput.text == "Location (Optional)"){
				locationTextInput.text = "";
				locationTextInput.textColor = 0x000000;
			}
			
			locationTextInput.dispatchEvent(new Event("LOCATION_SELECTED"));
		}
	}
}